package cn.edu.ctbu.scoremg.api;

import cn.edu.ctbu.scoremg.constant.REnum;
import cn.edu.ctbu.scoremg.entity.Teacher;
import cn.edu.ctbu.scoremg.exception.RException;
import cn.edu.ctbu.scoremg.service.TeacherService;
import cn.edu.ctbu.scoremg.util.RUtil;
import cn.edu.ctbu.scoremg.vo.QueryObj;
import cn.edu.ctbu.scoremg.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/teacher")
public class TeacherApiController {
    @Autowired
    private TeacherService teacherService;

    @GetMapping("/list")
    public R<List<Teacher>> findAll() {
        List<Teacher> teachers = teacherService.getAll();
        return RUtil.success(teachers);
    }

    @GetMapping("/list0")
    public R<List<Teacher>> findAll0() {
        List<Teacher> teachers = teacherService.getAll();
        return RUtil.success(teachers);
    }

    @GetMapping("/{id}")
    public R<Teacher> findById(@PathVariable int id) {
        Teacher teacher = teacherService.findById(id);
        return RUtil.success(teacher);
    }

    @GetMapping("/findByName")
    public R<List<Teacher>> findByName(@RequestParam String name) {
        return RUtil.success(teacherService.findByName(name));
    }

    @PostMapping("/add")
    public R<Teacher> add(@RequestBody Teacher teacher) {
        return RUtil.success(teacherService.add(teacher));
    }

    @PutMapping("/update")
    public R<Teacher> update(@RequestBody Teacher teacher) {
        return RUtil.success(teacherService.update(teacher));
    }

    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable Integer id) {
        teacherService.delete(id);
        return RUtil.success();
    }

    @GetMapping("/validateUser")
    public R validateTeacherNoAndPassword(String no, String password) throws Exception {
        return RUtil.success(teacherService.validateNoAndPassword(no, password));
    }

    @PostMapping("/deleteBatch")
    public R deleteBatch(@RequestBody List<Integer> ids) {
        try {
            teacherService.deleteBatch(ids);
            return RUtil.success();
        } catch (Exception e) {
            return RUtil.error(REnum.DELETE_ERR.getCode(), "批量删除失败");
        }
    }

    @PostMapping("/getbypage")
    public R<Page<Teacher>> getByPage(@RequestBody QueryObj<Teacher> qObj) {
        System.out.println("接收到的请求数据: " + qObj);

        // 按id排倒序
        Sort sort = Sort.by(Sort.Direction.DESC, "id");

        Integer pageIndex = 0;
        Integer pageSize = 10; // 一页10条数据

        if (qObj == null) {
            Pageable pageable = PageRequest.of(pageIndex, pageSize, sort);
            Page<Teacher> teachers = teacherService.getAll(pageable);
            return RUtil.success(teachers.getContent(), teachers.getTotalElements());
        } else {
            pageIndex = qObj.getPage() - 1;
            pageSize = qObj.getLimit();

            if (qObj.getData() instanceof Teacher) {
                Teacher teacher = (Teacher) qObj.getData();
                Pageable pageable = PageRequest.of(pageIndex, pageSize, sort);

                ExampleMatcher matcher = ExampleMatcher.matching()
                        .withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains())
                        .withIgnoreNullValues();

                Example<Teacher> example = Example.of(teacher, matcher);
                Page<Teacher> teacherPage = teacherService.getAll(example, pageable);

                return RUtil.success(teacherPage.getContent(), teacherPage.getTotalElements());
            } else {
                throw new RException(REnum.QUERY_ERR);
            }
        }
    }
}